/*
    SDL - Simple DirectMedia Layer
    Partial implementation of SDL library (originally written by
    Sam Lantinga <slouken@libsdl.org>) for the particular purpose to support
    Prequengine (http://code.google.com/p/prequengine/) on BlackBerry(tm)
    tablets and smartphones.

    Copyright (C) 2013  xlamsp

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

    xlamsp@gmail.com
*/

#include "SDL.h"

extern void SDL_StartTicks(void);

/* The current SDL version */
static SDL_version version = { SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL };

/* The initialized subsystems */
static Uint32 SDL_initialized = 0;
static Uint32 ticks_started = 0;

int SDL_InitSubSystem(Uint32 flags)
{
    SDL_Log(DEBUG1, "flags=0x%x", flags);

    /* Initialize the video/event subsystem */
    if((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
        if(SDL_VideoInit(NULL, 0) < 0) {
            return(-1);
        }
        SDL_initialized |= SDL_INIT_VIDEO;
    }

    /* Initialize the timer subsystem */
    if(!ticks_started) {
        SDL_StartTicks();
        ticks_started = 1;
    }
    if((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
        SDL_initialized |= SDL_INIT_TIMER;
    }

    #warning: implemet other subsystems

    return 0;
}

int SDL_Init(Uint32 flags)
{
    return SDL_InitSubSystem(flags);
}

void SDL_QuitSubSystem(Uint32 flags)
{
    SDL_Log(DEBUG1, "flags=0x%x", flags);

    /* Shut down requested initialized subsystems */
    if((flags & SDL_initialized & SDL_INIT_TIMER)) {
        SDL_initialized &= ~SDL_INIT_TIMER;
    }

    if((flags & SDL_initialized & SDL_INIT_VIDEO)) {
        SDL_VideoQuit();
        SDL_initialized &= ~SDL_INIT_VIDEO;
    }

    #warning: implemet other subsystems

    return;
}

extern void SDL_Quit(void)
{
    SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
    return;
}

const SDL_version * SDL_Linked_Version(void)
{
    return (&version);
}

